package com.android.duininsuoai;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.duininsuoai.activity.mine.BuyTicketActivity;
import com.android.duininsuoai.app.BaseActivity;
import com.android.duininsuoai.fragment.Fragment11;
import com.android.duininsuoai.fragment.Fragment4;
import com.android.duininsuoai.fragment.group.Fragment_DirectBuy;
import com.android.duininsuoai.service.WebSocketService;
import com.android.duininsuoai.service.WebSocketService1;
import com.android.duininsuoai.service.WebSocketService2;
import com.android.duininsuoai.service.WebSocketService3;
import com.android.duininsuoai.utils.ConstantsUrl;
import com.android.duininsuoai.utils.HttpUtils;
import com.android.duininsuoai.utils.MyLog;
import com.android.duininsuoai.utils.ObjectUtils;
import com.android.duininsuoai.utils.update.DownloadCallback;
import com.android.duininsuoai.utils.update.UpVersion;
import com.android.duininsuoai.utils.update.UpVersionDialog;

import okhttp3.Call;

/**
 * 主页
 */

public class MainActivity extends BaseActivity {

    private int index;
    private int currentTabIndex;
    private ImageView[] imagebuttons;
    private TextView[] textviews;
    private boolean isExit;
    private MainActivity mcontext;
    Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mcontext = MainActivity.this;
        initUI();
    }
    public void initUI() {
//        try {
//            OkhttpManager.getInstance().setTrustrCertificates(mcontext.getAssets().open("214267635160756.pem"));
//        }catch (IOException e)
//        {
//            e.printStackTrace();
//        }
        changeFragment();
       // setTabSelect(2);
        setTabSelect(0);
        Update();
    }

    private void Update() {


        String address = ConstantsUrl.domain + ConstantsUrl.MAIN_UPDATE_VERSION;
        MyLog.e("地址", address);
        HttpUtils.get(address, null, null, new DownloadCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(UpVersion response, int id) {

                if (response.getCode() == 0) {
                    if (ObjectUtils.getVersionCode(MainActivity.this) < Integer.parseInt(response.getData().getName())) {
                        UpVersionDialog upVersionDialog = new UpVersionDialog();
                        upVersionDialog.show(mcontext.getFragmentManager(), "upVersionDialog");
                    } else {

                    }
                } else {


                }
            }
        });


    }

    private DataChange dataChange;

    public void setData(DataChange dataChange) {
        this.dataChange = dataChange;
    }

    public interface DataChange {
        void setDataChange(int curr);
    }

    private void ONscroll(int curr) {
        if (dataChange != null) {
            dataChange.setDataChange(curr);
        }
    }

    private Fragment Fragment1, Fragment2, Fragment3, Fragment4, Fragment5;

    public void setTabSelect(int i) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (i) {
            case 0:
                Fragment1 = manager.findFragmentByTag("TAG1");
                hideTab(transaction);
                if (Fragment1 == null) {
                    Fragment1 = new Fragment11();
                    transaction.add(R.id.fragment_container, Fragment1, "TAG1");
                } else {
                    transaction.show(Fragment1);
                }
                break;
            case 1:
//                hideTab(transaction);
//                Fragment2 = new Fragment2();
//                transaction.replace(R.id.fragment_container, Fragment2);
                Intent intent_voucher = new Intent(mcontext,BuyTicketActivity.class);
                startActivityForResult(intent_voucher,0);
                break;
            case 2:
                Fragment3 = manager.findFragmentByTag("TAG3");
                hideTab(transaction);
                if (Fragment3 == null) {
                    Fragment3 = Fragment_DirectBuy.newInstance();
                    transaction.add(R.id.fragment_container, Fragment3, "TAG3");
                } else {
                    transaction.show(Fragment3);
                }
                break;
            case 3:
                Fragment4 = manager.findFragmentByTag("TAG4");
                hideTab(transaction);
                if (Fragment4 == null) {
                    Fragment4 = new Fragment4();
                    transaction.add(R.id.fragment_container, Fragment4, "TAG4");
                } else {
                    transaction.show(Fragment4);
                }
                break;

        }
        transaction.commitAllowingStateLoss();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {
            case 0:
//                index = 0;
//                imagebuttons[currentTabIndex].setSelected(false);
//                imagebuttons[index].setSelected(true);
//                textviews[currentTabIndex].setTextColor(0xFF333333);
//                textviews[index].setTextColor(0xFFFF2741);
//                currentTabIndex = index;
//                setTabSelect(0);
                break;
        }
    }

    private void hideTab(FragmentTransaction transaction) {
        if (Fragment1 != null) {
            transaction.hide(Fragment1);
        }
        if (Fragment2 != null) {
            transaction.hide(Fragment2);
        }
        if (Fragment3 != null) {
            transaction.hide(Fragment3);
        }
        if (Fragment4 != null) {
            transaction.hide(Fragment4);
        }
    }
    private void changeFragment() {
        imagebuttons = new ImageView[5];
        imagebuttons[0] = (ImageView) findViewById(R.id.mian_tab_img_1);
        imagebuttons[1] = (ImageView) findViewById(R.id.mian_tab_img_2);
        imagebuttons[2] = (ImageView) findViewById(R.id.mian_tab_img_3);
        imagebuttons[3] = (ImageView) findViewById(R.id.mian_tab_img_4);
        imagebuttons[0].setSelected(true);
        textviews = new TextView[5];
        textviews[0] = (TextView) findViewById(R.id.mian_tab_tv_1);
        textviews[1] = (TextView) findViewById(R.id.mian_tab_tv_2);
        textviews[2] = (TextView) findViewById(R.id.mian_tab_tv_3);
        textviews[3] = (TextView) findViewById(R.id.mian_tab_tv_4);
        textviews[0].setTextColor(0xFFFF2741);
    }
    //改变主界面的fragment
    public  void ChangeMainFragment(int type)
    {
        index = type;
        imagebuttons[currentTabIndex].setSelected(false);
        imagebuttons[index].setSelected(true);
        textviews[currentTabIndex].setTextColor(0xFF333333);
        textviews[index].setTextColor(0xFFFF2741);
        currentTabIndex = index;
        setTabSelect(type);
    }

    public void onTabClicked(View v) {
        switch (v.getId()) {
            case R.id.mian_tab_rl_1:
                index = 0;
                setTabSelect(0);
                break;
            case R.id.mian_tab_rl_2:
                index = 1;
                setTabSelect(1);
                break;
            case R.id.mian_tab_rl_3:
                index = 2;
                setTabSelect(2);
                break;
            case R.id.mian_tab_rl_4:

                index = 3;
                setTabSelect(3);
                break;

        }
        imagebuttons[currentTabIndex].setSelected(false);
        imagebuttons[index].setSelected(true);
        textviews[currentTabIndex].setTextColor(0xFF333333);
        textviews[index].setTextColor(0xFFFF2741);
        currentTabIndex = index;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isExit) {
                SharedPreferences sp = getSharedPreferences("time", 0);
                SharedPreferences.Editor edit = sp.edit();
                edit.clear();
                edit.commit();
                destroyService();
                System.exit(0);
            } else {
                ObjectUtils.toast(mcontext, "再按一次就可以退出了");
                isExit = true;
            }
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void destroyService(){
        Intent serviceIntent = new Intent(this, WebSocketService.class);
        Intent serviceIntent1 = new Intent(this, WebSocketService1.class);
        Intent serviceIntent2 = new Intent(this, WebSocketService2.class);
        Intent serviceIntent3 = new Intent(this, WebSocketService3.class);
        stopService(serviceIntent);
        stopService(serviceIntent1);
        stopService(serviceIntent2);
        stopService(serviceIntent3);
    }

}